<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="日志频率类型" prop="rate">
        <el-select
          v-model="queryParams.rate"
          @change="rateType"
          placeholder="请选择日志频率类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.cps_rate_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工作日志类型" prop="titleType">
        <el-select
          v-model="queryParams.titleType"
          placeholder="请选择工作日志类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.clr_work_log_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="时间" prop="dateRange" v-if="queryParams.rate == 1">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item
        label="时间"
        prop="statisticsDate"
        v-if="queryParams.rate == 2"
      >
        <el-date-picker
          v-model="queryParams.statisticsDate"
          type="week"
          format="yyyy 第 WW 周"
          @change="setWeekNum"
        ></el-date-picker>
      </el-form-item>
      <el-form-item
        label="时间"
        prop="statisticsDate"
        v-if="queryParams.rate == 3"
      >
        <el-date-picker
          v-model="queryParams.statisticsDate"
          type="month"
          value-format="yyyy-MM"
        ></el-date-picker>
      </el-form-item>
      <el-form-item
        label="时间"
        prop="statisticsDate"
        v-if="queryParams.rate == 5"
      >
        <el-date-picker
          v-model="queryParams.statisticsDate"
          type="year"
          value-format="yyyy"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- <searchForm
      :formOptions="formOptions"
      @onSearch="onSearch"
      @onReset="onReset"
      :formLabelWidth="formLabelWidth"
      v-show="showSearch"
    ></searchForm>-->

    <el-table
      v-loading="loading"
      :data="workLogList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="工作日志类型" align="center" prop="titleType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.clr_work_log_type"
            :value="scope.row.titleType"
          />
        </template>
      </el-table-column>
      <el-table-column label="日志频率类型" align="center" prop="rate">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cps_rate_type"
            :value="scope.row.rate"
          />
        </template>
      </el-table-column>
      <el-table-column label="统计日期" align="center" prop="fillingDate">
      </el-table-column>
      <!-- <el-table-column label="统计日期" align="center" prop="fillingDate" v-if="this.queryParams.rate == 2">
      </el-table-column> -->
      <el-table-column label="已填报" align="center" prop="completed">
        <template slot-scope="scope">
          <el-button type="text" @click="completedPeople(scope.row)" style="cursor:pointer;">{{
            scope.row.completed
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="未填报" align="center" prop="notCompleted">
        <template slot-scope="scope">
          <el-button type="text" @click="notCompletedPeople(scope.row)" style="cursor:pointer;">{{
            scope.row.notCompleted
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="需要填报" align="center" prop="total" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="people" width="50%" append-to-body>
      <el-table :data="peopleList">
        <el-table-column label="序号" align="center" type="index" />
        <el-table-column
          label="姓名"
          align="center"
          prop="clrName"
        ></el-table-column>
        <el-table-column
          label="教工号"
          align="center"
          prop="tno"
        ></el-table-column>
        <el-table-column
          label="院系"
          align="center"
          prop="deptName"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="联系方式" align="center" prop="phone" />
      </el-table>
      <pagination
        v-show="peopleTotal > 0"
        :total="peopleTotal"
        :page.sync="peopleParams.pageNum"
        :limit.sync="peopleParams.pageSize"
        @pagination="completedPeoples"
      />
    </el-dialog>
  </div>
</template>

<script>
import { statisticsWorkLog, clrList, notClrList } from "@/api/clr/workLog";
import { getDicts } from "@/api/system/dict/type";
import searchForm from "@/components/Search/searchForm.vue";
import fileUpload from "@/components/FileUpload/index";
export default {
  name: "WorkLog",
  components: { fileUpload, searchForm },
  dicts: ["clr_work_log_type", "cps_rate_type"],
  data() {
    return {
      title: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 辅导员工作日志表格数据
      workLogList: [],
      dateRange: [],
      peopleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rate: "1",
        titleType: null,
        fillingDate: null,
        statisticsDate: "",
      },
      peopleTotal: 0,
      peopleParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      people: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取搜索表单提交的数据
    handleQuery() {
      // 查询条件和分页数据合并
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询辅导员工作日志列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.rate == "1") {
        this.queryParams = this.addDateRange(this.queryParams, this.dateRange);
        this.queryParams.statisticsDate = "";
        statisticsWorkLog(this.queryParams).then((response) => {
          this.workLogList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      } else {
        this.queryParams.params.beginTime = "";
        this.queryParams.params.endTime = "";
        console.log(this.dateRange, this.queryParams);
        statisticsWorkLog(this.queryParams).then((response) => {
          this.workLogList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    rateType() {
      this.dateRange = [];
    },
    // dateTime() {
    //   console.log("dateTime", this.dateRange)
    //   this.queryParams.beginTime = this.dateRange[0]
    //   this.queryParams.endTime = this.dateRange[1]
    // },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.cwlId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 已填报人数
    completedPeople(row) {
      this.peopleList = [];
      this.title = "已填报人数";
      console.log(row);
      if (row.fillingDate) {
        this.peopleParams.rate = row.rate;
        this.peopleParams.titleType = row.titleType;
        this.peopleParams.fillingDate = row.fillingDate.substring(0, 10);
      }
      this.people = true;

      clrList(this.peopleParams).then(res => {
        if (res.code == '200') {
        console.log("res", res);
        this.peopleList = res.rows;
        this.peopleTotal = res.total;
      }
      });
      
    },
    // 已填报人数
    completedPeoples() {
      if (this.title == "未填报人数") {
        this.peopleList = [];
        notClrList(this.peopleParams).then((res) => {
          if (res.code == 200) {
            this.peopleList = res.rows;
            this.peopleTotal = res.total;
            console.log("res", this.peopleList, res);
          }
        });
      } else {
        this.peopleList = [];
        let res = clrList(this.peopleParams).then((res) => {
          if (res.code == 200) {
            console.log("res", res);
            this.peopleList = res.rows;
            this.peopleTotal = res.total;
          }
        });
      }
    },
    // 未填报人数
    async notCompletedPeople(row) {
      this.peopleList = [];
      this.title = "未填报人数";
      this.people = true;
      console.log(row);
      this.peopleParams.rate = row.rate;
      this.peopleParams.titleType = row.titleType;
      this.peopleParams.fillingDate = row.fillingDate.substring(0, 10);
      let res = await notClrList(this.peopleParams);
      if (res.code == 200) {
        console.log("res", res);
        this.peopleList = res.rows;
        this.peopleTotal = res.total;
      }
    },
    p(s) {
      return s < 10 ? "0" + s : s;
    },

    setWeekNum() {
      // console.log("res", this.queryParams.statisticsDate)
      const newTime = this.queryParams.statisticsDate.getTime();
      const day = this.queryParams.statisticsDate.getDay();
      const oneDayTime = 24 * 60 * 60 * 1000;
      const fromTime = newTime - day * oneDayTime;
      const endTime = newTime + (7 - day) * oneDayTime;
      const fromDate = new Date(fromTime);
      this.startDate =
        fromDate.getFullYear() +
        "-" +
        this.p(fromDate.getMonth() + 1) +
        "-" +
        this.p(fromDate.getDate() + 1);
      const toDate = new Date(endTime);
      this.endDate =
        toDate.getFullYear() +
        "\xa0" +
        this.p(toDate.getMonth() + 1) +
        "\xa0" +
        this.p(toDate.getDate());
      console.log(this.startDate);
      this.queryParams.statisticsDate = this.startDate;
      console.log(this.endDate);
      this.finalDate = this.startDate + "-" + this.endDate;
      console.log(this.queryParams.statisticsDate);
    },
  },
};
</script>
